{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "id": "EuGbkREYqu6n"
   },
   "source": [
    "# Metal Vector Store"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "_D2M6RiFq3zp"
   },
   "source": [
    "## Creating a Metal Vector Store"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "3iAr1k9Rrjg1"
   },
   "source": [
    "1. Register an account for [Metal](https://app.getmetal.io/)\n",
    "2. Generate an API key in [Metal's Settings](https://app.getmetal.io/settings/organization). Save the `api_key` + `client_id`\n",
    "3. Generate an Index in [Metal's Dashboard](https://app.getmetal.io/). Save the `index_id`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "EUPtAhnGsQ7I"
   },
   "source": [
    "## Load data into your Index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "id": "lJ3PobNlq8PC"
   },
   "outputs": [],
   "source": [
    "import logging\n",
    "import sys\n",
    "\n",
    "logging.basicConfig(stream=sys.stdout, level=logging.INFO)\n",
    "logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "id": "aQb6tXm4sxMZ"
   },
   "outputs": [],
   "source": [
    "from llama_index import VectorStoreIndex, SimpleDirectoryReader\n",
    "from llama_index.vector_stores import MetalVectorStore\n",
    "from IPython.display import Markdown, display"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "id": "a8ae33cFszV7"
   },
   "outputs": [],
   "source": [
    "# load documents\n",
    "documents = SimpleDirectoryReader(\"../paul_graham_essay/data\").load_data()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "id": "AOcOxebas1We"
   },
   "outputs": [],
   "source": [
    "# initialize Metal Vector Store\n",
    "from llama_index.storage.storage_context import StorageContext\n",
    "\n",
    "api_key = \"api key\"\n",
    "client_id = \"client id\"\n",
    "index_id = \"index id\"\n",
    "\n",
    "vector_store = MetalVectorStore(\n",
    "    api_key=api_key,\n",
    "    client_id=client_id,\n",
    "    index_id=index_id,\n",
    ")\n",
    "storage_context = StorageContext.from_defaults(vector_store=vector_store)\n",
    "index = VectorStoreIndex.from_documents(documents, storage_context=storage_context)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "cVz4ADaFtxgg"
   },
   "source": [
    "## Query Index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "dIgc_nA-tzzZ"
   },
   "outputs": [],
   "source": [
    "# set Logging to DEBUG for more detailed outputs\n",
    "query_engine = index.as_query_engine()\n",
    "response = query_engine.query(\"What did the author do growing up?\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "7vbx3mBkt3V8"
   },
   "outputs": [],
   "source": [
    "display(Markdown(f\"<b>{response}</b>\"))"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
